package com.iuweb.user.controller;


import com.iuweb.commen.cst.Const;
import com.iuweb.commen.domain.MenuResult;
import com.iuweb.commen.domain.Resource;
import com.iuweb.commen.domain.ShareUserResources;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ruiwu
 * @version V1.0
 * @Description:用户Controller层
 */
@Controller
@RequestMapping("/common/user")
public class UserController {
    private final static Logger log = LoggerFactory.getLogger(UserController.class);

    /**
     * 权限控制，先略过
     * @param request
     * @param map
     * @return
     */
    @RequestMapping(method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ModelAndView main(HttpServletRequest request, Map<String, Object> map) {

        return new ModelAndView("wrapper.jsp.user.showAllUser");
    }


    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/getMenu", method = RequestMethod.GET)
    @ResponseBody
    public MenuResult getMenu(HttpServletRequest request) {
        HttpSession session = request.getSession();
        List<Resource> resources = new ArrayList<Resource>();
        List<ShareUserResources> resourceList = (List<ShareUserResources>) session.getAttribute(Const.SESSION_MENU_KEY);
        List<Resource> children = new ArrayList<Resource>();
        List<Resource> parents = new ArrayList<Resource>();
        if (resourceList != null) {
            for (ShareUserResources shareUserResource : resourceList) {
                Resource resource = new Resource();
                resource.setName(shareUserResource.getResourceName());
                resource.setAccessUrl(shareUserResource.getResourceUrl());
                resource.setPid(shareUserResource.getParentRefid());
                resource.setCode(shareUserResource.getRefid());
                resource.setIconClass(shareUserResource.getIconClass());
                resource.setUniqueCode(shareUserResource.getUniqueCode());
                String type = shareUserResource.getResourceType();
                if (type != null && "file".equals(type)) {
                    parents.add(resource);
                } else {
                    children.add(resource);
                }
            }
            for (Resource resourceParent : parents) {
                String patrentCode = resourceParent.getCode();
                for (Resource resourceChild : children) {
                    String pCode = resourceChild.getPid();
                    if (pCode.equals(patrentCode)) {
                        List<Resource> pchildren = resourceParent.getChildren();
                        if (pchildren == null) {
                            pchildren = new ArrayList<Resource>();
                        }
                        pchildren.add(resourceChild);
                        resourceParent.setChildren(pchildren);
                    }
                }
            }
            resources = parents;
        }

        MenuResult result = new MenuResult();
        result.setResources(resources);
        return result;
    }
    /**
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "showAllUser",method= RequestMethod.GET)
    public ModelAndView showAllUser(Model model){
        return new ModelAndView("page.jsp.user.showAllUser");
    }
}
